package com.whosly.mysql.protocol;

import com.google.common.primitives.Bytes;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * Authentication plugin data for MySQL.
 *
 * <p>
 *     The auth-plugin-data is the concatenation of strings auth-plugin-data-part-1 and auth-plugin-data-part-2.
 *     The auth-plugin-data-part-1's length is 8; The auth-plugin-data-part-2's length is 12.
 * </p>
 *
 * @author fengyang(yueny09@163.com)
 * @date 2023年06月29日 上午10:32
 */
@RequiredArgsConstructor
@Getter
public final class MySQLAuthPluginData {
    private static String fixedSeed = "CLYWNn86";
    private static String restOfScrambleBuff = "nnwAN816Omyh";

    /**
     * fixedSeedByte
     * seed 8
     */
    private final byte[] authPluginDataPart1;

    /**
     * fixedRestOfScrambleBuffByte
     * <p>
     * scramble_buff 12
     */
    private final byte[] authPluginDataPart2;

    public MySQLAuthPluginData() {
        this(fixedSeed, restOfScrambleBuff);
    }

    public MySQLAuthPluginData(String fixedSeed, String restOfScrambleBuff) {
        this(fixedSeed.getBytes(), restOfScrambleBuff.getBytes());

//        this(MySQLRandomGenerator.getINSTANCE().generateRandomBytes(8),
//                MySQLRandomGenerator.getINSTANCE().generateRandomBytes(12));
    }

    /**
     * Get authentication plugin data.
     *
     * @return authentication plugin data
     */
    public byte[] getAuthenticationPluginData() {
        return Bytes.concat(authPluginDataPart1, authPluginDataPart2);
    }

}

